package com.coffee.soft.controller;

import com.alibaba.fastjson.JSON;
import com.coffee.soft.entity.ItemsShow;
import com.coffee.soft.service.ItemsEatDrinkDiy;
import com.coffee.soft.service.ItemsEatDrinkDiyFirst;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * @Author:LiuRuidong
 * @Description: 吃点啥，喝点啥，DIY及换一换
 * 测试已通过
 * @Date: Created in 0:31 2018/2/6
 * @Modified By:
 */
@Controller
public class ItemsEatDrinkDiyController {
    @Autowired
    private ItemsEatDrinkDiyFirst itemsEatDrinkDiyFirst;
    @Autowired
    private ItemsEatDrinkDiy itemsEatDrinkDiy;

    /**
     * @Author：LiuRuidong
     * @Description: 默认加载吃点啥
     * @Date:2018/2/6 0:34
     * @Param:
     * @return
     */
    @RequestMapping("/eatItems")
    @ResponseBody
    public void  eatItemsHandler(HttpServletRequest request, HttpServletResponse response) {
        String callback = request.getParameter("callback");
        List<ItemsShow> itemsShowList =itemsEatDrinkDiyFirst.eatItems();
        String json = JSON.toJSONString(itemsShowList);
        try {
            response.setCharacterEncoding("utf-8");
            response.setContentType("text/html;charset=utf-8");
            PrintWriter pw = response.getWriter();
            if ((callback!="")&&(callback!=null)){
                json=callback+"("+json+")";
            }
            pw.println(json);
            pw.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    /**
     * @Author：LiuRuidong
     * @Description: 默认加载喝点啥
     * @Date:2018/2/6 0:35
     * @Param:
     * @return
     */
    @RequestMapping("/drinkItems")
    @ResponseBody
    public void  drinkItemsHandler(HttpServletRequest request, HttpServletResponse response) {
        String callback = request.getParameter("callback");
        List<ItemsShow> itemsShowList =itemsEatDrinkDiyFirst.drinkItems();
        String json = JSON.toJSONString(itemsShowList);
        try {
            response.setCharacterEncoding("utf-8");
            response.setContentType("text/html;charset=utf-8");
            PrintWriter pw = response.getWriter();
            if ((callback!="")&&(callback!=null)){
                json=callback+"("+json+")";
            }
            pw.println(json);
            pw.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    /**
     * @Author：LiuRuidong
     * @Description: 默认加载DIY商品
     * @Date:2018/2/6 0:36
     * @Param:
     * @return
     */
    @RequestMapping("/diyItems")
    @ResponseBody
    public void  diyItemsHandler(HttpServletRequest request, HttpServletResponse response) {
        List<ItemsShow> itemsShowList =itemsEatDrinkDiyFirst.diyItems();
        String callback = request.getParameter("callback");
        String json = JSON.toJSONString(itemsShowList);
        try {
            response.setCharacterEncoding("utf-8");
            response.setContentType("text/html;charset=utf-8");
            PrintWriter pw = response.getWriter();
            if ((callback!="")&&(callback!=null)){
                json=callback+"("+json+")";
            }
            pw.println(json);
            pw.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    /**
     * @Author：LiuRuidong
     * @Description: 换一换吃点啥
     * @Date:2018/2/6 0:36
     * @Param:
     * @return
     */
    @RequestMapping("/changeEatItems")
    @ResponseBody
    public void  eatItemsHandler(HttpServletRequest request, HttpServletResponse response,String itemsId) {

        List<ItemsShow> itemsShowList =itemsEatDrinkDiy.eatItems(itemsId);
        String callback = request.getParameter("callback");
        String json = JSON.toJSONString(itemsShowList);
        try {
            response.setCharacterEncoding("utf-8");
            response.setContentType("text/html;charset=utf-8");
            PrintWriter pw = response.getWriter();
            if ((callback!="")&&(callback!=null)){
                json=callback+"("+json+")";
            }
            pw.println(json);
            pw.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * @Author：LiuRuidong
     * @Description: 换一换喝点啥
     * @Date:2018/2/6 0:36
     * @Param:
     * @return
     */
    @RequestMapping("/changeDrinkItems")
    @ResponseBody
    public void  changeDrinkItemsHandler(HttpServletRequest request, HttpServletResponse response,String itemsId) {
        List<ItemsShow> itemsShowList =itemsEatDrinkDiy.drinkItems(itemsId);
        String callback = request.getParameter("callback");
        String json = JSON.toJSONString(itemsShowList);
        try {
            response.setCharacterEncoding("utf-8");
            response.setContentType("text/html;charset=utf-8");
            PrintWriter pw = response.getWriter();
            if ((callback!="")&&(callback!=null)){
                json=callback+"("+json+")";
            }
            pw.println(json);
            pw.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    /**
     * @Author：LiuRuidong
     * @Description: 换一换DIY
     * @Date:2018/2/6 0:36
     * @Param:
     * @return
     */
    @RequestMapping("/changeDiyItems")
    @ResponseBody
    public void  changeDiyItemsHandler(HttpServletRequest request, HttpServletResponse response,String itemsId) {
        List<ItemsShow> itemsShowList =itemsEatDrinkDiy.diyItems(itemsId);
        String callback = request.getParameter("callback");
        String json = JSON.toJSONString(itemsShowList);
        try {
            response.setCharacterEncoding("utf-8");
            response.setContentType("text/html;charset=utf-8");
            PrintWriter pw = response.getWriter();
            if ((callback!="")&&(callback!=null)){
                json=callback+"("+json+")";
            }
            pw.println(json);
            pw.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
